'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      * Created by Jacky.gao on 2016/6/28.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      */


var _raphael = require('raphael');

var _raphael2 = _interopRequireDefault(_raphael);

var _undoManager = require('undo-manager');

var _undoManager2 = _interopRequireDefault(_undoManager);

var _SelectTool = require('./tools/SelectTool.js');

var _SelectTool2 = _interopRequireDefault(_SelectTool);

var _ConnectionTool = require('./tools/ConnectionTool.js');

var _ConnectionTool2 = _interopRequireDefault(_ConnectionTool);

var _Connection = require('./Connection.js');

var _Connection2 = _interopRequireDefault(_Connection);

var _event = require('./event.js');

var event = _interopRequireWildcard(_event);

var _Node = require('./Node.js');

var _Node2 = _interopRequireDefault(_Node);

var _uuid = require('uuid');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Context = function () {
    function Context(container) {
        _classCallCheck(this, Context);

        this.undoManager = new _undoManager2.default();
        this.toolsMap = new Map();
        this._initBuiltinTools();
        this.container = container;
        this.paper = new _raphael2.default(container[0], '100%', '100%');
        this.allFigures = [];
        this.selectionFigures = [];
        this.selectionRects = this.paper.set();
        this.selectionPaths = this.paper.set();
        this.currentConnection = null;
        this._initEvent();
    }

    _createClass(Context, [{
        key: 'selectFigure',
        value: function selectFigure(figure) {
            this.startSelect();
            this.addSelection(figure);
            this.endSelect();
        }
    }, {
        key: 'startSelect',
        value: function startSelect() {
            this.resetSelection();
        }
    }, {
        key: 'resizePaper',
        value: function resizePaper(newWidth, newHeight) {
            var w = this.container.width(),
                h = this.container.height();
            if (newWidth > w) {
                this.container.width(newWidth + 10);
            }
            if (newHeight > h) {
                this.container.height(newHeight + 10);
            }
        }
    }, {
        key: 'addRedoUndo',
        value: function addRedoUndo(redoUndo) {
            this.undoManager.add(redoUndo);
        }
    }, {
        key: 'addSelection',
        value: function addSelection(figure) {
            this.selectionFigures.push(figure);
            if (figure instanceof _Connection2.default) {
                this.selectionPaths.push(figure.path);
            } else {
                this.selectionRects.push(figure.rect);
            }
        }
    }, {
        key: 'endSelect',
        value: function endSelect() {
            this.selectionRects.attr("stroke", '#FF9800');
            this.selectionPaths.attr({ "stroke": '#999', 'stroke-dasharray': '20' });
            var firstSelectFigure = null;
            this.selectionFigures.forEach(function (figure, index) {
                if (!firstSelectFigure) {
                    firstSelectFigure = figure;
                }
                if (figure instanceof _Connection2.default) {
                    figure.select();
                }
            });
            if (firstSelectFigure) {
                event.eventEmitter.emit(event.OBJECT_SELECTED, firstSelectFigure);
            }
        }
    }, {
        key: 'resetSelection',
        value: function resetSelection() {
            this.selectionRects.attr("stroke", '#fff');
            this.selectionPaths.attr({ "stroke": '#999', 'stroke-dasharray': 'none' });
            this.selectionRects = this.paper.set();
            this.selectionPaths = this.paper.set();
            this.selectionFigures.forEach(function (figure, index) {
                if (figure instanceof _Connection2.default) {
                    figure.unSelect();
                }
            });
            this.selectionFigures.splice(0, this.selectionFigures.length);
            event.eventEmitter.emit(event.CANVAS_SELECTED);
        }
    }, {
        key: 'registerTool',
        value: function registerTool(tool) {
            var type = tool.getType();
            if (this.toolsMap.has(type)) {
                throw 'Figure [' + type + '] already exist.';
            }
            this.toolsMap.set(type, tool);
        }
    }, {
        key: 'nextUUID',
        value: function nextUUID() {
            return (0, _uuid.v4)();
        }
    }, {
        key: 'getFigureById',
        value: function getFigureById(id) {
            var target = void 0;
            var _iteratorNormalCompletion = true;
            var _didIteratorError = false;
            var _iteratorError = undefined;

            try {
                for (var _iterator = this.allFigures[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                    var figure = _step.value;

                    if (figure instanceof _Node2.default) {
                        if (figure.rect.id === id || figure.icon.id === id || figure.text.id === id) {
                            target = figure;
                            break;
                        }
                    }
                }
            } catch (err) {
                _didIteratorError = true;
                _iteratorError = err;
            } finally {
                try {
                    if (!_iteratorNormalCompletion && _iterator.return) {
                        _iterator.return();
                    }
                } finally {
                    if (_didIteratorError) {
                        throw _iteratorError;
                    }
                }
            }

            return target;
        }
    }, {
        key: 'getNodeByUUID',
        value: function getNodeByUUID(uuid) {
            var targetNode = void 0;
            var _iteratorNormalCompletion2 = true;
            var _didIteratorError2 = false;
            var _iteratorError2 = undefined;

            try {
                for (var _iterator2 = this.allFigures[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
                    var node = _step2.value;

                    if (node.uuid === uuid) {
                        targetNode = node;
                        break;
                    }
                }
            } catch (err) {
                _didIteratorError2 = true;
                _iteratorError2 = err;
            } finally {
                try {
                    if (!_iteratorNormalCompletion2 && _iterator2.return) {
                        _iterator2.return();
                    }
                } finally {
                    if (_didIteratorError2) {
                        throw _iteratorError2;
                    }
                }
            }

            return targetNode;
        }
    }, {
        key: 'removeFigureByUUID',
        value: function removeFigureByUUID(uuid) {
            var targetNode = this.getNodeByUUID(uuid);
            var pos = this.allFigures.indexOf(targetNode);
            this.allFigures.splice(pos, 1);
            targetNode.remove();
        }
    }, {
        key: 'cancelConnection',
        value: function cancelConnection() {
            if (this.currentConnection) {
                var fromConnections = this.currentConnection.from.fromConnections;
                var pos = fromConnections.indexOf(this.currentConnection);
                fromConnections.splice(pos, 1);
                this.currentConnection.path.remove();
                if (this.currentConnection.text) {
                    this.currentConnection.text.remove();
                }
            }
            this.currentConnection = null;
        }
    }, {
        key: '_initEvent',
        value: function _initEvent() {
            var _this2 = this;

            event.eventEmitter.on(event.TRIGGER_TOOL, function (nodeName) {
                if (!_this2.toolsMap.has(nodeName)) {
                    throw 'Node ' + nodeName + ' not exist.';
                }
                _this2.currentTool = _this2.toolsMap.get(nodeName);
                _this2.cancelConnection();
            });
            event.eventEmitter.on(event.REMOVE_CLICKED, function () {
                var selections = [].concat(_toConsumableArray(_this2.selectionFigures));
                if (selections === 0) {
                    return;
                }
                _this2.resetSelection();
                selections.forEach(function (select, index) {
                    var jsonData = select.toJSON(),
                        uuid = select.uuid,
                        _this = _this2;
                    if (select instanceof _Connection2.default) {
                        _this2.addRedoUndo({
                            redo: function redo() {
                                var conn = _this.getNodeByUUID(uuid);
                                var fromUUID = conn.from.uuid,
                                    toUUID = conn.to.uuid;
                                var fromNode = _this.context.getNodeByUUID(fromUUID),
                                    toNode = _this.context.getNodeByUUID(toUUID);
                                var newConnection = new _Connection2.default(fromNode, { x: fromNode.rect.attr('x'), y: fromNode.rect.attr('y') });
                                fromNode.fromConnections.push(newConnection);
                                toNode.toConnections.push(newConnection);
                                newConnection.to = toNode;
                                newConnection.fromJSON(jsonData);
                            },
                            undo: function undo() {
                                _this.removeFigureByUUID(uuid);
                            }
                        });
                    } else {
                        var tool = select._tool;
                        _this2.addRedoUndo({
                            redo: function redo() {
                                _this.removeFigureByUUID(uuid);
                            },
                            undo: function undo() {
                                var newNode = tool._newNodeInstance(10, 10, '');
                                if (newNode) {
                                    newNode.initFromJson(jsonData);
                                    newNode._buildConnections();
                                }
                            }
                        });
                    }
                    _this2.removeFigureByUUID(uuid);
                });
            });
            event.eventEmitter.on(event.ALIGN_LEFT, function () {
                var x = -1,
                    y = -1,
                    w = void 0,
                    map = new Map(),
                    index = 0;
                var minX = -1,
                    minWidth = -1;
                var _iteratorNormalCompletion3 = true;
                var _didIteratorError3 = false;
                var _iteratorError3 = undefined;

                try {
                    for (var _iterator3 = _this2.selectionFigures[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
                        var select = _step3.value;

                        if (select instanceof _Connection2.default) {
                            break;
                        }
                        w = select.rect.attr('width');
                        var currentX = select.rect.attr('x');
                        if (minX == -1) {
                            minX = currentX;
                            minWidth = w;
                        } else {
                            if (minX > currentX) {
                                minX = currentX;
                                minWidth = w;
                            }
                        }
                    }
                } catch (err) {
                    _didIteratorError3 = true;
                    _iteratorError3 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion3 && _iterator3.return) {
                            _iterator3.return();
                        }
                    } finally {
                        if (_didIteratorError3) {
                            throw _iteratorError3;
                        }
                    }
                }

                var _iteratorNormalCompletion4 = true;
                var _didIteratorError4 = false;
                var _iteratorError4 = undefined;

                try {
                    for (var _iterator4 = _this2.selectionFigures[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
                        var _select = _step4.value;

                        if (_select instanceof _Connection2.default) {
                            break;
                        }
                        w = _select.rect.attr('width');
                        var data = { oldXY: { x: _select.rect.attr('x'), y: _select.rect.attr('y') } };
                        _select.moveTo(minX + w / 2, y);
                        data.newXY = { x: _select.rect.attr('x'), y: _select.rect.attr('y') };
                        map.set(_select.uuid, data);
                        index++;
                    }
                } catch (err) {
                    _didIteratorError4 = true;
                    _iteratorError4 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion4 && _iterator4.return) {
                            _iterator4.return();
                        }
                    } finally {
                        if (_didIteratorError4) {
                            throw _iteratorError4;
                        }
                    }
                }

                var _this = _this2;
                _this2.addRedoUndo({
                    redo: function redo() {
                        var _iteratorNormalCompletion5 = true;
                        var _didIteratorError5 = false;
                        var _iteratorError5 = undefined;

                        try {
                            for (var _iterator5 = map.keys()[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
                                var uuid = _step5.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var newXY = data.newXY;

                                node.move(newXY.x, newXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError5 = true;
                            _iteratorError5 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion5 && _iterator5.return) {
                                    _iterator5.return();
                                }
                            } finally {
                                if (_didIteratorError5) {
                                    throw _iteratorError5;
                                }
                            }
                        }
                    },
                    undo: function undo() {
                        var _iteratorNormalCompletion6 = true;
                        var _didIteratorError6 = false;
                        var _iteratorError6 = undefined;

                        try {
                            for (var _iterator6 = map.keys()[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
                                var uuid = _step6.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var oldXY = data.oldXY;

                                node.move(oldXY.x, oldXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError6 = true;
                            _iteratorError6 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion6 && _iterator6.return) {
                                    _iterator6.return();
                                }
                            } finally {
                                if (_didIteratorError6) {
                                    throw _iteratorError6;
                                }
                            }
                        }
                    }
                });
            });
            event.eventEmitter.on(event.ALIGN_CENTER, function () {
                var x = -1,
                    y = -1,
                    w = void 0,
                    map = new Map(),
                    index = 0;
                var selectRects = [];
                //collect area
                var _iteratorNormalCompletion7 = true;
                var _didIteratorError7 = false;
                var _iteratorError7 = undefined;

                try {
                    for (var _iterator7 = _this2.selectionFigures[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
                        var select = _step7.value;

                        if (select instanceof _Connection2.default) {
                            break;
                        }
                        var center = select.rect.attr('y');
                        var height = select.rect.attr('height');
                        selectRects.push({ start: center - height / 2, end: center + height / 2, uuid: _this2.nextUUID() });
                    }
                    //judeg area mixed 
                } catch (err) {
                    _didIteratorError7 = true;
                    _iteratorError7 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion7 && _iterator7.return) {
                            _iterator7.return();
                        }
                    } finally {
                        if (_didIteratorError7) {
                            throw _iteratorError7;
                        }
                    }
                }

                var mixed = false;
                var _iteratorNormalCompletion8 = true;
                var _didIteratorError8 = false;
                var _iteratorError8 = undefined;

                try {
                    for (var _iterator8 = selectRects[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
                        var rect = _step8.value;
                        var _iteratorNormalCompletion13 = true;
                        var _didIteratorError13 = false;
                        var _iteratorError13 = undefined;

                        try {
                            for (var _iterator13 = selectRects[Symbol.iterator](), _step13; !(_iteratorNormalCompletion13 = (_step13 = _iterator13.next()).done); _iteratorNormalCompletion13 = true) {
                                var rect2 = _step13.value;

                                if (rect.uuid != rect2.uuid && (rect.start >= rect2.start && rect.start <= rect2.end || rect.end >= rect2.start && rect.end <= rect2.end)) {
                                    mixed = true;
                                    break;
                                }
                            }
                        } catch (err) {
                            _didIteratorError13 = true;
                            _iteratorError13 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion13 && _iterator13.return) {
                                    _iterator13.return();
                                }
                            } finally {
                                if (_didIteratorError13) {
                                    throw _iteratorError13;
                                }
                            }
                        }

                        if (mixed) {
                            break;
                        }
                    }
                } catch (err) {
                    _didIteratorError8 = true;
                    _iteratorError8 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion8 && _iterator8.return) {
                            _iterator8.return();
                        }
                    } finally {
                        if (_didIteratorError8) {
                            throw _iteratorError8;
                        }
                    }
                }

                if (!mixed) {
                    var _iteratorNormalCompletion9 = true;
                    var _didIteratorError9 = false;
                    var _iteratorError9 = undefined;

                    try {
                        for (var _iterator9 = _this2.selectionFigures[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
                            var _select2 = _step9.value;

                            if (_select2 instanceof _Connection2.default) {
                                break;
                            }
                            var data = { oldXY: { x: _select2.rect.attr('x'), y: _select2.rect.attr('y') } };
                            if (index === 0) {
                                x = _select2.rect.attr('x'), w = _select2.rect.attr('width');
                                x += w / 2;
                            } else {
                                _select2.moveTo(x, y);
                            }
                            data.newXY = { x: _select2.rect.attr('x'), y: _select2.rect.attr('y') };
                            map.set(_select2.uuid, data);
                            index++;
                        }
                    } catch (err) {
                        _didIteratorError9 = true;
                        _iteratorError9 = err;
                    } finally {
                        try {
                            if (!_iteratorNormalCompletion9 && _iterator9.return) {
                                _iterator9.return();
                            }
                        } finally {
                            if (_didIteratorError9) {
                                throw _iteratorError9;
                            }
                        }
                    }
                } else {
                    var _iteratorNormalCompletion10 = true;
                    var _didIteratorError10 = false;
                    var _iteratorError10 = undefined;

                    try {
                        for (var _iterator10 = _this2.selectionFigures[Symbol.iterator](), _step10; !(_iteratorNormalCompletion10 = (_step10 = _iterator10.next()).done); _iteratorNormalCompletion10 = true) {
                            var _select3 = _step10.value;

                            if (_select3 instanceof _Connection2.default) {
                                break;
                            }
                            var _data = { oldXY: { x: _select3.rect.attr('x'), y: _select3.rect.attr('y') } };
                            var height = void 0;
                            if (index === 0) {
                                x = _select3.rect.attr('x');
                                y = _select3.rect.attr('y');
                            } else {
                                height = _select3.rect.attr('height');
                                y = y + height / 2 + 100;
                                _select3.move(x, y, _select3.rect.attr('width'), height);
                            }
                            _data.newXY = { x: _select3.rect.attr('x'), y: _select3.rect.attr('y') };
                            map.set(_select3.uuid, _data);
                            index++;
                        }
                    } catch (err) {
                        _didIteratorError10 = true;
                        _iteratorError10 = err;
                    } finally {
                        try {
                            if (!_iteratorNormalCompletion10 && _iterator10.return) {
                                _iterator10.return();
                            }
                        } finally {
                            if (_didIteratorError10) {
                                throw _iteratorError10;
                            }
                        }
                    }
                }
                var _this = _this2;
                _this2.addRedoUndo({
                    redo: function redo() {
                        var _iteratorNormalCompletion11 = true;
                        var _didIteratorError11 = false;
                        var _iteratorError11 = undefined;

                        try {
                            for (var _iterator11 = map.keys()[Symbol.iterator](), _step11; !(_iteratorNormalCompletion11 = (_step11 = _iterator11.next()).done); _iteratorNormalCompletion11 = true) {
                                var uuid = _step11.value;

                                var node = _this.getNodeByUUID(uuid),
                                    _data2 = map.get(uuid);
                                var newXY = _data2.newXY;

                                node.move(newXY.x, newXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError11 = true;
                            _iteratorError11 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion11 && _iterator11.return) {
                                    _iterator11.return();
                                }
                            } finally {
                                if (_didIteratorError11) {
                                    throw _iteratorError11;
                                }
                            }
                        }
                    },
                    undo: function undo() {
                        var _iteratorNormalCompletion12 = true;
                        var _didIteratorError12 = false;
                        var _iteratorError12 = undefined;

                        try {
                            for (var _iterator12 = map.keys()[Symbol.iterator](), _step12; !(_iteratorNormalCompletion12 = (_step12 = _iterator12.next()).done); _iteratorNormalCompletion12 = true) {
                                var uuid = _step12.value;

                                var node = _this.getNodeByUUID(uuid),
                                    _data3 = map.get(uuid);
                                var oldXY = _data3.oldXY;

                                node.move(oldXY.x, oldXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError12 = true;
                            _iteratorError12 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion12 && _iterator12.return) {
                                    _iterator12.return();
                                }
                            } finally {
                                if (_didIteratorError12) {
                                    throw _iteratorError12;
                                }
                            }
                        }
                    }
                });
            });
            event.eventEmitter.on(event.ALIGN_RIGHT, function () {
                var x = -1,
                    y = -1,
                    w = void 0,
                    map = new Map(),
                    index = 0;
                var maxX = -1;
                var _iteratorNormalCompletion14 = true;
                var _didIteratorError14 = false;
                var _iteratorError14 = undefined;

                try {
                    for (var _iterator14 = _this2.selectionFigures[Symbol.iterator](), _step14; !(_iteratorNormalCompletion14 = (_step14 = _iterator14.next()).done); _iteratorNormalCompletion14 = true) {
                        var select = _step14.value;

                        if (select instanceof _Connection2.default) {
                            break;
                        }
                        w = select.rect.attr('width');
                        var currentX = select.rect.attr('x') + w;
                        if (maxX == -1) {
                            maxX = currentX;
                        } else {
                            if (maxX < currentX) {
                                maxX = currentX;
                            }
                        }
                    }
                } catch (err) {
                    _didIteratorError14 = true;
                    _iteratorError14 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion14 && _iterator14.return) {
                            _iterator14.return();
                        }
                    } finally {
                        if (_didIteratorError14) {
                            throw _iteratorError14;
                        }
                    }
                }

                var _iteratorNormalCompletion15 = true;
                var _didIteratorError15 = false;
                var _iteratorError15 = undefined;

                try {
                    for (var _iterator15 = _this2.selectionFigures[Symbol.iterator](), _step15; !(_iteratorNormalCompletion15 = (_step15 = _iterator15.next()).done); _iteratorNormalCompletion15 = true) {
                        var _select4 = _step15.value;

                        if (_select4 instanceof _Connection2.default) {
                            break;
                        }
                        w = _select4.rect.attr('width');
                        var data = { oldXY: { x: _select4.rect.attr('x'), y: _select4.rect.attr('y') } };
                        _select4.moveTo(maxX - w / 2, y);
                        data.newXY = { x: _select4.rect.attr('x'), y: _select4.rect.attr('y') };
                        map.set(_select4.uuid, data);
                        index++;
                    }
                } catch (err) {
                    _didIteratorError15 = true;
                    _iteratorError15 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion15 && _iterator15.return) {
                            _iterator15.return();
                        }
                    } finally {
                        if (_didIteratorError15) {
                            throw _iteratorError15;
                        }
                    }
                }

                var _this = _this2;
                _this2.addRedoUndo({
                    redo: function redo() {
                        var _iteratorNormalCompletion16 = true;
                        var _didIteratorError16 = false;
                        var _iteratorError16 = undefined;

                        try {
                            for (var _iterator16 = map.keys()[Symbol.iterator](), _step16; !(_iteratorNormalCompletion16 = (_step16 = _iterator16.next()).done); _iteratorNormalCompletion16 = true) {
                                var uuid = _step16.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var newXY = data.newXY;

                                node.move(newXY.x, newXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError16 = true;
                            _iteratorError16 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion16 && _iterator16.return) {
                                    _iterator16.return();
                                }
                            } finally {
                                if (_didIteratorError16) {
                                    throw _iteratorError16;
                                }
                            }
                        }
                    },
                    undo: function undo() {
                        var _iteratorNormalCompletion17 = true;
                        var _didIteratorError17 = false;
                        var _iteratorError17 = undefined;

                        try {
                            for (var _iterator17 = map.keys()[Symbol.iterator](), _step17; !(_iteratorNormalCompletion17 = (_step17 = _iterator17.next()).done); _iteratorNormalCompletion17 = true) {
                                var uuid = _step17.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var oldXY = data.oldXY;

                                node.move(oldXY.x, oldXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError17 = true;
                            _iteratorError17 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion17 && _iterator17.return) {
                                    _iterator17.return();
                                }
                            } finally {
                                if (_didIteratorError17) {
                                    throw _iteratorError17;
                                }
                            }
                        }
                    }
                });
            });
            event.eventEmitter.on(event.ALIGN_TOP, function () {
                var x = -1,
                    y = -1,
                    h = void 0,
                    map = new Map(),
                    index = 0;
                var minY = -1;
                var _iteratorNormalCompletion18 = true;
                var _didIteratorError18 = false;
                var _iteratorError18 = undefined;

                try {
                    for (var _iterator18 = _this2.selectionFigures[Symbol.iterator](), _step18; !(_iteratorNormalCompletion18 = (_step18 = _iterator18.next()).done); _iteratorNormalCompletion18 = true) {
                        var select = _step18.value;

                        if (select instanceof _Connection2.default) {
                            break;
                        }
                        var currentY = select.rect.attr('y');
                        if (minY == -1) {
                            minY = currentY;
                        } else {
                            if (minY > currentY) {
                                minY = currentY;
                            }
                        }
                    }
                } catch (err) {
                    _didIteratorError18 = true;
                    _iteratorError18 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion18 && _iterator18.return) {
                            _iterator18.return();
                        }
                    } finally {
                        if (_didIteratorError18) {
                            throw _iteratorError18;
                        }
                    }
                }

                var _iteratorNormalCompletion19 = true;
                var _didIteratorError19 = false;
                var _iteratorError19 = undefined;

                try {
                    for (var _iterator19 = _this2.selectionFigures[Symbol.iterator](), _step19; !(_iteratorNormalCompletion19 = (_step19 = _iterator19.next()).done); _iteratorNormalCompletion19 = true) {
                        var _select5 = _step19.value;

                        if (_select5 instanceof _Connection2.default) {
                            break;
                        }
                        h = _select5.rect.attr('height');
                        var data = { oldXY: { x: _select5.rect.attr('x'), y: _select5.rect.attr('y') } };
                        _select5.moveTo(x, minY + h / 2);
                        data.newXY = { x: _select5.rect.attr('x'), y: _select5.rect.attr('y') };
                        map.set(_select5.uuid, data);
                        index++;
                    }
                } catch (err) {
                    _didIteratorError19 = true;
                    _iteratorError19 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion19 && _iterator19.return) {
                            _iterator19.return();
                        }
                    } finally {
                        if (_didIteratorError19) {
                            throw _iteratorError19;
                        }
                    }
                }

                var _this = _this2;
                _this2.addRedoUndo({
                    redo: function redo() {
                        var _iteratorNormalCompletion20 = true;
                        var _didIteratorError20 = false;
                        var _iteratorError20 = undefined;

                        try {
                            for (var _iterator20 = map.keys()[Symbol.iterator](), _step20; !(_iteratorNormalCompletion20 = (_step20 = _iterator20.next()).done); _iteratorNormalCompletion20 = true) {
                                var uuid = _step20.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var newXY = data.newXY;

                                node.move(newXY.x, newXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError20 = true;
                            _iteratorError20 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion20 && _iterator20.return) {
                                    _iterator20.return();
                                }
                            } finally {
                                if (_didIteratorError20) {
                                    throw _iteratorError20;
                                }
                            }
                        }
                    },
                    undo: function undo() {
                        var _iteratorNormalCompletion21 = true;
                        var _didIteratorError21 = false;
                        var _iteratorError21 = undefined;

                        try {
                            for (var _iterator21 = map.keys()[Symbol.iterator](), _step21; !(_iteratorNormalCompletion21 = (_step21 = _iterator21.next()).done); _iteratorNormalCompletion21 = true) {
                                var uuid = _step21.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var oldXY = data.oldXY;

                                node.move(oldXY.x, oldXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError21 = true;
                            _iteratorError21 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion21 && _iterator21.return) {
                                    _iterator21.return();
                                }
                            } finally {
                                if (_didIteratorError21) {
                                    throw _iteratorError21;
                                }
                            }
                        }
                    }
                });
            });
            event.eventEmitter.on(event.ALIGN_MIDDLE, function () {
                var x = -1,
                    y = -1,
                    w = void 0,
                    h = void 0,
                    map = new Map(),
                    index = 0;
                var selectRects = [];
                //collect area
                var _iteratorNormalCompletion22 = true;
                var _didIteratorError22 = false;
                var _iteratorError22 = undefined;

                try {
                    for (var _iterator22 = _this2.selectionFigures[Symbol.iterator](), _step22; !(_iteratorNormalCompletion22 = (_step22 = _iterator22.next()).done); _iteratorNormalCompletion22 = true) {
                        var select = _step22.value;

                        if (select instanceof _Connection2.default) {
                            break;
                        }
                        var center = select.rect.attr('x');
                        var width = select.rect.attr('width');
                        selectRects.push({ start: center - width / 2, end: center + width / 2, uuid: _this2.nextUUID() });
                    }
                    //judeg area mixed 
                } catch (err) {
                    _didIteratorError22 = true;
                    _iteratorError22 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion22 && _iterator22.return) {
                            _iterator22.return();
                        }
                    } finally {
                        if (_didIteratorError22) {
                            throw _iteratorError22;
                        }
                    }
                }

                var mixed = false;
                var _iteratorNormalCompletion23 = true;
                var _didIteratorError23 = false;
                var _iteratorError23 = undefined;

                try {
                    for (var _iterator23 = selectRects[Symbol.iterator](), _step23; !(_iteratorNormalCompletion23 = (_step23 = _iterator23.next()).done); _iteratorNormalCompletion23 = true) {
                        var rect = _step23.value;
                        var _iteratorNormalCompletion28 = true;
                        var _didIteratorError28 = false;
                        var _iteratorError28 = undefined;

                        try {
                            for (var _iterator28 = selectRects[Symbol.iterator](), _step28; !(_iteratorNormalCompletion28 = (_step28 = _iterator28.next()).done); _iteratorNormalCompletion28 = true) {
                                var rect2 = _step28.value;

                                if (rect.uuid != rect2.uuid && (rect.start >= rect2.start && rect.start <= rect2.end || rect.end >= rect2.start && rect.end <= rect2.end)) {
                                    mixed = true;
                                    break;
                                }
                            }
                        } catch (err) {
                            _didIteratorError28 = true;
                            _iteratorError28 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion28 && _iterator28.return) {
                                    _iterator28.return();
                                }
                            } finally {
                                if (_didIteratorError28) {
                                    throw _iteratorError28;
                                }
                            }
                        }

                        if (mixed) {
                            break;
                        }
                    }
                } catch (err) {
                    _didIteratorError23 = true;
                    _iteratorError23 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion23 && _iterator23.return) {
                            _iterator23.return();
                        }
                    } finally {
                        if (_didIteratorError23) {
                            throw _iteratorError23;
                        }
                    }
                }

                if (!mixed) {
                    var _iteratorNormalCompletion24 = true;
                    var _didIteratorError24 = false;
                    var _iteratorError24 = undefined;

                    try {
                        for (var _iterator24 = _this2.selectionFigures[Symbol.iterator](), _step24; !(_iteratorNormalCompletion24 = (_step24 = _iterator24.next()).done); _iteratorNormalCompletion24 = true) {
                            var _select6 = _step24.value;

                            if (_select6 instanceof _Connection2.default) {
                                break;
                            }
                            var data = { oldXY: { x: _select6.rect.attr('x'), y: _select6.rect.attr('y') } };
                            if (index === 0) {
                                y = _select6.rect.attr('y'), h = _select6.rect.attr('height');
                                y += h / 2;
                            } else {
                                _select6.moveTo(x, y);
                            }
                            data.newXY = { x: _select6.rect.attr('x'), y: _select6.rect.attr('y') };
                            map.set(_select6.uuid, data);
                            index++;
                        }
                    } catch (err) {
                        _didIteratorError24 = true;
                        _iteratorError24 = err;
                    } finally {
                        try {
                            if (!_iteratorNormalCompletion24 && _iterator24.return) {
                                _iterator24.return();
                            }
                        } finally {
                            if (_didIteratorError24) {
                                throw _iteratorError24;
                            }
                        }
                    }
                } else {
                    var _iteratorNormalCompletion25 = true;
                    var _didIteratorError25 = false;
                    var _iteratorError25 = undefined;

                    try {
                        for (var _iterator25 = _this2.selectionFigures[Symbol.iterator](), _step25; !(_iteratorNormalCompletion25 = (_step25 = _iterator25.next()).done); _iteratorNormalCompletion25 = true) {
                            var _select7 = _step25.value;

                            if (_select7 instanceof _Connection2.default) {
                                break;
                            }
                            var _data4 = { oldXY: { x: _select7.rect.attr('x'), y: _select7.rect.attr('y') } };
                            if (index === 0) {
                                x = _select7.rect.attr('x');
                                y = _select7.rect.attr('y');
                            } else {
                                w = _select7.rect.attr('width');
                                x = x + w / 2 + 100;
                                _select7.move(x, y, w, _select7.rect.attr('height'));
                            }
                            _data4.newXY = { x: _select7.rect.attr('x'), y: _select7.rect.attr('y') };
                            map.set(_select7.uuid, _data4);
                            index++;
                        }
                    } catch (err) {
                        _didIteratorError25 = true;
                        _iteratorError25 = err;
                    } finally {
                        try {
                            if (!_iteratorNormalCompletion25 && _iterator25.return) {
                                _iterator25.return();
                            }
                        } finally {
                            if (_didIteratorError25) {
                                throw _iteratorError25;
                            }
                        }
                    }
                }
                var _this = _this2;
                _this2.addRedoUndo({
                    redo: function redo() {
                        var _iteratorNormalCompletion26 = true;
                        var _didIteratorError26 = false;
                        var _iteratorError26 = undefined;

                        try {
                            for (var _iterator26 = map.keys()[Symbol.iterator](), _step26; !(_iteratorNormalCompletion26 = (_step26 = _iterator26.next()).done); _iteratorNormalCompletion26 = true) {
                                var uuid = _step26.value;

                                var node = _this.getNodeByUUID(uuid),
                                    _data5 = map.get(uuid);
                                var newXY = _data5.newXY;

                                node.move(newXY.x, newXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError26 = true;
                            _iteratorError26 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion26 && _iterator26.return) {
                                    _iterator26.return();
                                }
                            } finally {
                                if (_didIteratorError26) {
                                    throw _iteratorError26;
                                }
                            }
                        }
                    },
                    undo: function undo() {
                        var _iteratorNormalCompletion27 = true;
                        var _didIteratorError27 = false;
                        var _iteratorError27 = undefined;

                        try {
                            for (var _iterator27 = map.keys()[Symbol.iterator](), _step27; !(_iteratorNormalCompletion27 = (_step27 = _iterator27.next()).done); _iteratorNormalCompletion27 = true) {
                                var uuid = _step27.value;

                                var node = _this.getNodeByUUID(uuid),
                                    _data6 = map.get(uuid);
                                var oldXY = _data6.oldXY;

                                node.move(oldXY.x, oldXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError27 = true;
                            _iteratorError27 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion27 && _iterator27.return) {
                                    _iterator27.return();
                                }
                            } finally {
                                if (_didIteratorError27) {
                                    throw _iteratorError27;
                                }
                            }
                        }
                    }
                });
            });
            event.eventEmitter.on(event.ALIGN_BOTTOM, function () {
                var x = -1,
                    y = -1,
                    h = void 0,
                    map = new Map(),
                    index = 0;
                var maxY = -1;
                var _iteratorNormalCompletion29 = true;
                var _didIteratorError29 = false;
                var _iteratorError29 = undefined;

                try {
                    for (var _iterator29 = _this2.selectionFigures[Symbol.iterator](), _step29; !(_iteratorNormalCompletion29 = (_step29 = _iterator29.next()).done); _iteratorNormalCompletion29 = true) {
                        var select = _step29.value;

                        if (select instanceof _Connection2.default) {
                            break;
                        }
                        h = select.rect.attr('height');
                        var currentY = select.rect.attr('y') + h;
                        if (maxY == -1) {
                            maxY = currentY;
                        } else {
                            if (maxY < currentY) {
                                maxY = currentY;
                            }
                        }
                    }
                } catch (err) {
                    _didIteratorError29 = true;
                    _iteratorError29 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion29 && _iterator29.return) {
                            _iterator29.return();
                        }
                    } finally {
                        if (_didIteratorError29) {
                            throw _iteratorError29;
                        }
                    }
                }

                var _iteratorNormalCompletion30 = true;
                var _didIteratorError30 = false;
                var _iteratorError30 = undefined;

                try {
                    for (var _iterator30 = _this2.selectionFigures[Symbol.iterator](), _step30; !(_iteratorNormalCompletion30 = (_step30 = _iterator30.next()).done); _iteratorNormalCompletion30 = true) {
                        var _select8 = _step30.value;

                        if (_select8 instanceof _Connection2.default) {
                            break;
                        }
                        h = _select8.rect.attr('height');
                        var data = { oldXY: { x: _select8.rect.attr('x'), y: _select8.rect.attr('y') } };
                        _select8.moveTo(x, maxY - h / 2);
                        data.newXY = { x: _select8.rect.attr('x'), y: _select8.rect.attr('y') };
                        map.set(_select8.uuid, data);
                        index++;
                    }
                } catch (err) {
                    _didIteratorError30 = true;
                    _iteratorError30 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion30 && _iterator30.return) {
                            _iterator30.return();
                        }
                    } finally {
                        if (_didIteratorError30) {
                            throw _iteratorError30;
                        }
                    }
                }

                var _this = _this2;
                _this2.addRedoUndo({
                    redo: function redo() {
                        var _iteratorNormalCompletion31 = true;
                        var _didIteratorError31 = false;
                        var _iteratorError31 = undefined;

                        try {
                            for (var _iterator31 = map.keys()[Symbol.iterator](), _step31; !(_iteratorNormalCompletion31 = (_step31 = _iterator31.next()).done); _iteratorNormalCompletion31 = true) {
                                var uuid = _step31.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var newXY = data.newXY;

                                node.move(newXY.x, newXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError31 = true;
                            _iteratorError31 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion31 && _iterator31.return) {
                                    _iterator31.return();
                                }
                            } finally {
                                if (_didIteratorError31) {
                                    throw _iteratorError31;
                                }
                            }
                        }
                    },
                    undo: function undo() {
                        var _iteratorNormalCompletion32 = true;
                        var _didIteratorError32 = false;
                        var _iteratorError32 = undefined;

                        try {
                            for (var _iterator32 = map.keys()[Symbol.iterator](), _step32; !(_iteratorNormalCompletion32 = (_step32 = _iterator32.next()).done); _iteratorNormalCompletion32 = true) {
                                var uuid = _step32.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var oldXY = data.oldXY;

                                node.move(oldXY.x, oldXY.y, node.rect.attr('width'), node.rect.attr('height'));
                            }
                        } catch (err) {
                            _didIteratorError32 = true;
                            _iteratorError32 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion32 && _iterator32.return) {
                                    _iterator32.return();
                                }
                            } finally {
                                if (_didIteratorError32) {
                                    throw _iteratorError32;
                                }
                            }
                        }
                    }
                });
            });
            event.eventEmitter.on(event.UNIFY_SIZE, function () {
                var w = void 0,
                    h = void 0,
                    map = new Map(),
                    index = 0;
                var _iteratorNormalCompletion33 = true;
                var _didIteratorError33 = false;
                var _iteratorError33 = undefined;

                try {
                    for (var _iterator33 = _this2.selectionFigures[Symbol.iterator](), _step33; !(_iteratorNormalCompletion33 = (_step33 = _iterator33.next()).done); _iteratorNormalCompletion33 = true) {
                        var select = _step33.value;

                        if (select instanceof _Connection2.default) {
                            break;
                        }
                        var data = { oldWH: { w: select.rect.attr('width'), h: select.rect.attr('height') } };
                        if (index === 0) {
                            w = select.rect.attr('width'), h = select.rect.attr('height');
                        } else {
                            select.changeSize(w, h);
                        }
                        data.newWH = { w: select.rect.attr('width'), h: select.rect.attr('height') };
                        map.set(select.uuid, data);
                        index++;
                    }
                } catch (err) {
                    _didIteratorError33 = true;
                    _iteratorError33 = err;
                } finally {
                    try {
                        if (!_iteratorNormalCompletion33 && _iterator33.return) {
                            _iterator33.return();
                        }
                    } finally {
                        if (_didIteratorError33) {
                            throw _iteratorError33;
                        }
                    }
                }

                var _this = _this2;
                _this2.addRedoUndo({
                    redo: function redo() {
                        var _iteratorNormalCompletion34 = true;
                        var _didIteratorError34 = false;
                        var _iteratorError34 = undefined;

                        try {
                            for (var _iterator34 = map.keys()[Symbol.iterator](), _step34; !(_iteratorNormalCompletion34 = (_step34 = _iterator34.next()).done); _iteratorNormalCompletion34 = true) {
                                var uuid = _step34.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var newWH = data.newWH;
                                node.changeSize(newWH.w, newWH.h);
                            }
                        } catch (err) {
                            _didIteratorError34 = true;
                            _iteratorError34 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion34 && _iterator34.return) {
                                    _iterator34.return();
                                }
                            } finally {
                                if (_didIteratorError34) {
                                    throw _iteratorError34;
                                }
                            }
                        }
                    },
                    undo: function undo() {
                        var _iteratorNormalCompletion35 = true;
                        var _didIteratorError35 = false;
                        var _iteratorError35 = undefined;

                        try {
                            for (var _iterator35 = map.keys()[Symbol.iterator](), _step35; !(_iteratorNormalCompletion35 = (_step35 = _iterator35.next()).done); _iteratorNormalCompletion35 = true) {
                                var uuid = _step35.value;

                                var node = _this.getNodeByUUID(uuid),
                                    data = map.get(uuid);
                                var oldWH = data.oldWH;
                                node.changeSize(oldWH.w, oldWH.h);
                            }
                        } catch (err) {
                            _didIteratorError35 = true;
                            _iteratorError35 = err;
                        } finally {
                            try {
                                if (!_iteratorNormalCompletion35 && _iterator35.return) {
                                    _iterator35.return();
                                }
                            } finally {
                                if (_didIteratorError35) {
                                    throw _iteratorError35;
                                }
                            }
                        }
                    }
                });
            });
        }
    }, {
        key: '_initBuiltinTools',
        value: function _initBuiltinTools() {
            this.selectTool = new _SelectTool2.default();
            this.connectionTool = new _ConnectionTool2.default();
            this.selectTool.context = this;
            this.connectionTool.context = this;
            this.currentTool = this.selectTool;
        }
    }]);

    return Context;
}();

exports.default = Context;